// 产品详情页-基本信息模块：包括头图、价格、标题等
define(function(require, exports, module) {
    var $ = require('zepto');
    var dialog = require('dialog');

    function DepartureInfo() {}

    DepartureInfo.prototype.init = function(Calendar, Manager) {
        var self = this;
        this.aNum = 2;
        this.cNum = 0;
        this.calendar = Calendar;
        this.manager = new Manager();
        this.calendar.init();
        this.html = $("#departure div:eq(0)");
        var getDateInfo = {};

        this.calendar.afterPickedDate = function(date, node) {
            self.date = date;
            showCalendar(self.html);


            var dateInfo = getDateInfo[date];
            var showPriceStr = "";
            var endPriceStr = "最低优惠价<span>￥{{endPrice}}</span> = ";
            var tuniuPriceStr = "途牛价<span>￥" + dateInfo.adultPrice + "</span>";
            var maxPriceStr = "－最大优惠金额<span>￥{{maxPrice}}</span>";
            var vouchersPriceStr = "－可用抵用券<span>￥{{vouchersPrice}}</span>";

            $("#favorablePrice").next().html(dateInfo.promotionIntro);

            $("#adultPrice").html("￥" + dateInfo.adultPrice);
            $("#childPrice span:eq(0)").html("￥" + dateInfo.childPrice);
            $("#childPrice span:eq(1)").show();
            $("#roomPrice").html("￥" + dateInfo.roomAddBudget);

            // yinqian - 修改为不可选择儿童价
            if (parseInt(dateInfo.childPrice) == 0 || dateInfo.childPrice.length == 0) {
                $("#childPrice span:eq(0)").html("不支持儿童");
                $("#childPrice span:eq(1)").hide();
                self.cNum = 0;
                $('.child-num').find("input").val(0);
                $('.child-num').addClass('disabled');
                dialog({
                    type: 'info',
                    message: '本线路团期不接受儿童出游'
                });
            } else {
                $('.child-num').removeClass('disabled');
            }

            if (dateInfo.discountDesc && dateInfo.discountDesc.length > 0) {
                showPriceStr = showPriceStr + endPriceStr + tuniuPriceStr
                var endPrice = dateInfo.adultPrice;
                var maxPrice = 0;
                var vouchersPrice = 0;

                //dateInfo.discountDesc.discountType == 0 为最大优惠金额， 为1是可用抵用券
                for (var i = 0; i < dateInfo.discountDesc.length; i++) {
                    if (dateInfo.discountDesc[i].discountType == 0) {
                        showPriceStr = showPriceStr + maxPriceStr;
                        endPrice = endPrice - dateInfo.discountDesc[i].discountPrice;
                        maxPrice = dateInfo.discountDesc[i].discountPrice;
                        showPriceStr = showPriceStr.replace("{{maxPrice}}", maxPrice);
                    }
                    if (dateInfo.discountDesc[i].discountType == 1) {
                        showPriceStr = showPriceStr + vouchersPriceStr;
                        endPrice = endPrice - dateInfo.discountDesc[i].discountPrice;
                        vouchersPrice = dateInfo.discountDesc[i].discountPrice;
                        showPriceStr = showPriceStr.replace("{{vouchersPrice}}", vouchersPrice);
                    }
                }
                showPriceStr = showPriceStr.replace("{{endPrice}}", endPrice);
                $("#favorablePrice").html(showPriceStr);
            }
        }
        if ($.isEmptyObject(calendarPriceInfo)) {
            $('#departure').addClass('hide');
            $('#empty-departure').removeClass('hide');
            $('#discuss').addClass('disabled')
        }
        for (var key in calendarPriceInfo) {
            for (var date in calendarPriceInfo[key]) {
                getDateInfo[date] = calendarPriceInfo[key][date];
            }
        }
        this.bindEvent();
    }

    DepartureInfo.prototype.bindEvent = function() {
        var self = this;
        $("#hideLayer").on("click", function() {
            $("#maskLayer").hide();
            $("#departure").html(self.html);
            $('#btnOrderMiddle').one('click', function() {
                window.showCalendar();
            });
        })
        $(".addNum").delegate("div", "click", function(e) {
            if ($(this).parents(".addNum").hasClass('disabled')) {
                return;
            }

            var ele = e.target;
            var parent = $(ele).parents("td");
            var index = parent.index();
            var input = parent.find("input");
            if ($(ele).hasClass("minus")) {
                if (parseInt(input.val()) <= 0) {
                    return;
                };
                if (index == 0 && parseInt(input.val()) == 1) {
                    return;
                }
                input.val(parseInt(input.val()) - 1);
            }
            if ($(ele).hasClass("plus")) {
                input.val(parseInt(input.val()) + 1);
            };
            if (index == 0) {
                self.aNum = parseInt(input.val());
            } else {
                self.cNum = parseInt(input.val());
            }
        });

        $('#discuss .title').on('click', function() {
            if (document.body.clientWidth >= 768 || $('#discuss').hasClass('disabled')) {
                return;
            }
            if (typeof productType != "undefined" && (productType == 1 || productType == 8)) {
                //kxp add new onlineBook page
                var url = location.host;
                var param = self.manager.encode(JSON.stringify({"routeId": parseInt(productId)}));
                if (location.host.indexOf("dynamic") == -1) {
                    url = "http://dynamic." + url;
                    location.href = url + "/olb/order/gt/1/" + param;
                } else {
                    location.href = "/olb/order/gt/1/" + param;
                }

            }
        });

        //游客点评跳转优化
        $('#remark .layout div:first-child').on('click', function() {
            if (document.body.clientWidth >= 768) {
                return;
            }
            if (typeof productType != "undefined" && (productType == 1 || productType == 8)) {
                location.href = $('#remark .layout div:first-child').attr("data-href");
            }
        });

        $("#submit").click(function() {
            if (isAbroad) {
                // yinqian - 如果是出境，在线预订流程走全新controller
                var goto2 = $("#goto2GT").val() + "/params/";
            } else {
                var goto2 = $('#goto2').val() + "/";
            }
            var submitData = self.getSubmitData();
            if (!submitData.beginDate) {
                dialog({
                    type: 'info',
                    align: 'center',
                    message: "请先选择团期"
                });
                return false;
            }

            submitData = self.manager.encode(JSON.stringify(submitData));
            location.href = goto2 + submitData;
        });


        // 小屏的时候，当游客点评没有的时候，隐藏掉游客点评
        if ($('#empty-remark-all').css('display') == 'none') {
            if (document.documentElement.clientWidth < 767) {
                $('#remark').hide();
            }

        }
    }

    function showCalendar(html) {
        if (!html) {
            html = $("#departure div:eq(0)");
        }
        $("#maskLayer").show();
        $("#discussLayout .panel-content").html(html);
    }

    window.showCalendar = showCalendar;

    DepartureInfo.prototype.getSubmitData = function() {
        return {
            routeId: productId,
            beginDate: this.date,
            adultNum: this.aNum,
            childNum: this.cNum
        };
    }

    module.exports = DepartureInfo;
});